package com.power.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.Notice;
import com.power.model.Result;
import com.power.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 公告业务管理控制层
 * @Author dingchunlin
 * @Date 2024/07/17 21:01
 * @Version 1.0.0
 */
@Api(tags = "公告业务接口管理")
@RestController
@RequestMapping("/shop/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    /**
     * 多条件分页查询公告列表
     * @param current 页码
     * @param size   页大小
     * @param title  标题
     * @param status 状态
     * @param isTop 是否置顶
     * @return 公告列表
     */
    @ApiOperation(value = "多条件分页查询公告列表")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('shop:notice:page')")
    public Result<Page<Notice>> loadNoticePage(@RequestParam Long current,
                                                @RequestParam Long size,
                                                @RequestParam(required = false) String title,
                                                @RequestParam(required = false) Integer status,
                                                @RequestParam(required = false) Integer isTop) {
        // 创建分页对象
        Page<Notice> page = new Page<>(current, size);
        // 查询条件
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<Notice>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(status), Notice::getStatus, status);
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(isTop), Notice::getIsTop, isTop);
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(title), Notice::getTitle, title);
        lambdaQueryWrapper.orderByDesc(Notice::getCreateTime);
        // 查询公告列表
        page = noticeService.page(page, lambdaQueryWrapper);
        return Result.success(page);
    }

    /**
     * 新增公告
     * @param notice 公告实体
     * @return 操作结果
     */
    @ApiOperation(value = "新增公告")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('shop:notice:save')")
    public Result<String> saveNotice(@RequestBody Notice notice) {
        Boolean save = noticeService.saveNotice(notice);
        return Result.handle(save);
    }

    /**
     * 根据标识查询公告
     * @param noticeId 公告标识
     * @return 公告实体
     */
    @ApiOperation(value = "根据标识查询公告")
    @GetMapping("/info/{noticeId}")
    @PreAuthorize("hasAnyAuthority('shop:notice:info')")
    public Result<Notice> loadNoticeInfo(@PathVariable Long noticeId) {
        Notice notice = noticeService.getById(noticeId);
        return Result.success(notice);
    }

    /**
     * 修改公告信息
     * @param notice 公告实体
     * @return 操作结果
     */
    @ApiOperation(value = "修改公告信息")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('shop:notice:update')")
    public Result<String> modifyNotice(@RequestBody Notice notice) {
        Boolean modified = noticeService.modifyNotice(notice);
        return Result.handle(modified);
    }

    /**
     * 根据标识删除公告
     * @param noticeId 公告标识
     * @return 操作结果
     */
    @ApiOperation(value = "根据标识删除公告")
    @DeleteMapping("/{noticeId}")
    @PreAuthorize("hasAnyAuthority('shop:notice:delete')")
    public Result<String> removeNotice(@PathVariable Long noticeId) {
        Boolean remove = noticeService.removeById(noticeId);
        return Result.handle(remove);
    }

    /**
     * 查询置顶公告列表
     * @return 置顶公告列表
     */
    @ApiOperation(value = "查询小程序置顶公告列表")
    @GetMapping("/topNoticeList")
    public Result<List<Notice>> loadTopWxTopNoticeList() {
        List<Notice> topNoticeList = noticeService.queryTopWxTopNoticeList();
        return Result.success(topNoticeList);
    }

    /**
     * 查询小程序所有公告列表
     * @return 小程序所有公告列表
     */
    @ApiOperation(value = "查询小程序所有公告列表")
    @GetMapping("/noticeList")
    public Result<List<Notice>> loadWxAllNoticeList() {
        List<Notice> allNoticeList = noticeService.queryWxAllNoticeList();
        return Result.success(allNoticeList);
    }

    /**
     * 根据公告id公告详情
     * @param noticeId 公告id
     * @return 商品信息
     */
    @ApiOperation(value = "根据公告id查询下面的商品信息")
    @GetMapping("/detail/{noticeId}")
    public Result<Notice> loadWxNoticeInfo(@PathVariable Long noticeId) {
        Notice notice = noticeService.getById(noticeId);
        return Result.success(notice);
    }
}
